/*
 * Copyright (C) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef IMAGE_KNIFE_C_KEKE_IMAGEKNIFE_EGL_H
#define IMAGE_KNIFE_C_KEKE_IMAGEKNIFE_EGL_H

#include "log.h"
#include <EGL/egl.h>
#include <EGL/eglext.h>


namespace ImageKnifePro {
class ImageKnifeEGL {
public:
    /**
     * @brief 初始化EGL上下文，同一线程内不必重复初始化
     * 不同线程EGL 上下文环境不能复用
     * @return 初始化成功返回true， 初始化失败返回false
     */
    bool Init()
    {
        const EGLint confAttr[] = {
            EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT_KHR, EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
            EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_ALPHA_SIZE, 8,
            EGL_DEPTH_SIZE, 16, EGL_STENCIL_SIZE, 8, EGL_NONE
        };
        const EGLint ctxAttr[] = {
            EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE
        };
        const EGLint surfaceAttr[] = {
            EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE
        };

        EGLint eglMajVers;
        EGLint eglMinVers;
        EGLint numConfigs;
        eglDisplay_ = eglGetDisplay(EGL_DEFAULT_DISPLAY);
        // 初始化egl
        eglInitialize(eglDisplay_, &eglMajVers, &eglMinVers);
        IMAGE_KNIFE_CHECK_RETURN(eglDisplay_ == EGL_NO_DISPLAY, false);

        // 获取 EGLConfig 对象，确定渲染表面的配置信息
        if (!eglChooseConfig(eglDisplay_, confAttr, &eglConf_, 1, &numConfigs)) {
            IMAGE_KNIFE_LOG(LOG_ERROR, "Get EGL Config Failed");
            return false;
        }

        // 创建渲染表面 EGLSurface 使用 eglCreateBufferSurface 创建屏幕外渲染区域
        eglSurface_ = eglCreatePbufferSurface(eglDisplay_, eglConf_, surfaceAttr);
        IMAGE_KNIFE_CHECK_RETURN(eglSurface_ == EGL_NO_SURFACE, false);

        // 创建渲染上下文 EGLContext
        eglCtx_ = eglCreateContext(eglDisplay_, eglConf_, EGL_NO_CONTEXT, ctxAttr);
        IMAGE_KNIFE_CHECK_RETURN(eglCtx_ == EGL_NO_CONTEXT, false);

        // 绑定上下文
        if (!eglMakeCurrent(eglDisplay_, eglSurface_, eglSurface_, eglCtx_)) {
            IMAGE_KNIFE_LOG(LOG_ERROR, "EGL Bing Context Failed");
            return false;
        }

        return true;
    }

    ~ImageKnifeEGL()
    {
        // 释放EGL环境
        if (eglDisplay_ != EGL_NO_DISPLAY) {
            eglMakeCurrent(eglDisplay_, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
            if (eglCtx_ != EGL_NO_CONTEXT) {
                eglDestroyContext(eglDisplay_, eglCtx_);
            }

            if (eglSurface_ != EGL_NO_SURFACE) {
                eglDestroySurface(eglDisplay_, eglSurface_);
            }
            eglReleaseThread();
            eglTerminate(eglDisplay_);
        }
    }
private:
    EGLConfig eglConf_ = EGL_NO_CONFIG_KHR;
    EGLSurface eglSurface_ = EGL_NO_SURFACE;
    EGLContext eglCtx_ = EGL_NO_CONTEXT;
    EGLDisplay eglDisplay_ = EGL_NO_DISPLAY;
};
}

#endif //IMAGE_KNIFE_C_KEKE_IMAGEKNIFE_EGL_H
